package com.sweets.look.task;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.webkit.WebView;

import com.sweets.look.R;
import com.sweets.look.ui.widget.MarkProgressDialog;
import com.sweets.look.util.BrowserUtils;
import com.sweets.look.util.ViewUtils;


public class ScreenshotTask extends AsyncTask<Void, Void, Boolean> {
    private float contentHeight;
    private final Context context;
    private MarkProgressDialog dialog;
    private final boolean isAll;
    private String path;
    private String title;
    private final WebView webView;
    private int windowWidth;

    public ScreenshotTask(Context context, WebView webView, boolean isAll) {
        super();
        this.context = context;
        this.dialog = new MarkProgressDialog(context);
        this.dialog = null;
        this.webView = webView;
        this.isAll = isAll;
        this.windowWidth = 0;
        this.contentHeight = 0f;
        this.title = null;
        this.path = null;
    }

    @Override
    protected Boolean doInBackground(Void[] params) {

        boolean v2 = false;
        if (this.isAll) {
            try {
                this.path = BrowserUtils.screenshot(this.context, BrowserUtils.captureAll(this.webView, ((
                                float) this.windowWidth), this.contentHeight, false, Bitmap.Config.RGB_565),
                        this.title);
            } catch (Exception e) {
                this.path = null;
            }

        } else {
            try {
                this.path = BrowserUtils.screenshot(this.context, BrowserUtils.captureVisible(this.webView),
                        this.title);
            } catch (Exception e) {
                this.path = null;
            }
        }

        if (this.path != null && !this.path.isEmpty()) {
            v2 = true;
        }

        return v2;
    }


    @Override
    protected void onPostExecute(Boolean result) {
        this.dialog.dismiss();
        if (result) {
            ViewUtils.showToast(this.context.getString(R.string.toast_save_to) + this.path, context);
        } else {
            ViewUtils.showToast(this.context.getString(R.string.toast_operation_failed), context);
        }
    }


    @Override
    protected void onPreExecute() {
        this.dialog = new MarkProgressDialog(this.context);
        this.dialog.builder();
        this.dialog.setTitle(this.context.getString(R.string.action_screenshot));
        this.dialog.setMessage(this.context.getString(R.string.toast_wait_a_minute));
        this.dialog.setCancelable(false);
        this.dialog.show();
        this.windowWidth = ViewUtils.getWindowWidth(this.context);
        this.contentHeight = (((float) this.webView.getContentHeight())) * ViewUtils.getDensity(this.
                context);
        this.title = this.webView.getTitle();
    }
}

